﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using YArchitech.LIB;

namespace YArchitech.Controls
{
	public partial class AboutBox : System.Windows.Forms.Form
	{
		public AboutBox()
		{
			this.InitializeComponent();
			this.textBoxTrialInfo.Visible = false;
			this.textBoxUpdateInfo.Visible = false;
			this.textboxUpdateInfoSize = this.textBoxUpdateInfo.Size;
			this.FormSize = base.Size;
		}

		private string GetTrialTypeString()
		{
			if (IniHelper.GetSetting("StartUp", "MainTitle", Product.DefaultConfigFile, "盈建科 BIM 解决方案").Contains("教育"))
			{
				return "教育版";
			}
			return "试用版";
		}

		private void AboutBox_Load(object sender, EventArgs e)
		{
			string softWareName = Product.GetSoftWareName();
			string softWareBuildVer = Product.GetSoftWareBuildVer();
			this.textBoxProductNameVer.Text = softWareName + " " + softWareBuildVer;
			this.SetCkeckStartVisible(false);
			if (Product.IsArchitecture())
			{
				this.cbStartSame.Checked = Product.BeStartingAtTheSameTime();
				this.cbStartJust.Checked = Product.BeJustStarting();
				this.cbStartSame.Text = "启动Revit时，同时启动乐建。";
				this.cbStartJust.Text = "盈建科软件图标启动时，只启动乐建。";
				this.SetCkeckStartVisible(true);
			}
			else if (!Product.IsTunnel() && !Product.IsPC())
			{
				if (Product.IsDec())
				{
					this.cbStartSame.Checked = Product.BeStartingAtTheSameTime();
					this.cbStartJust.Checked = Product.BeJustStarting();
					this.cbStartSame.Text = "启动Revit时，同时启动盈建科装饰。";
					this.cbStartJust.Text = "盈建科软件图标启动时，只启动盈建科装饰。";
					this.SetCkeckStartVisible(true);
				}
				else if (Product.IsEDD())
				{
					this.cbStartSame.Checked = Product.BeStartingAtTheSameTime();
					this.cbStartJust.Checked = Product.BeJustStarting();
					this.cbStartSame.Text = "启动Revit时，同时启动机电深化。";
					this.cbStartJust.Text = "盈建科软件图标启动时，只启动机电深化。";
					this.SetCkeckStartVisible(true);
				}
				else if (!Product.IsMEI() && !Product.IsFamliyLeader() && Product.IsRME())
				{
					this.cbStartSame.Checked = Product.BeStartingAtTheSameTime();
					this.cbStartJust.Checked = Product.BeJustStarting();
					this.cbStartSame.Text = "启动Revit时，同时启动当前选项。";
					this.cbStartJust.Text = "盈建科软件图标启动时，只启动当前选项。";
					this.SetCkeckStartVisible(true);
				}
			}
			this.pictureBox1.BackgroundImage = Resources.HYRegister;
			this.pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
			string trialTypeString = this.GetTrialTypeString();
			string text = "正式版";
			this.comboBoxType.Items.Clear();
			this.comboBoxType.Items.Add(trialTypeString);
			this.comboBoxType.Items.Add(text);
			int softWareType = Product.GetSoftWareType();
			if (1 == softWareType)
			{
				this.comboBoxType.Text = trialTypeString;
				int trialExpirationType = Product.GetTrialExpirationType();
				if (1 == trialExpirationType)
				{
					this.textBoxTrialInfo.Text = string.Format("剩余试用天数为 {0} 天", Product.GetTrialExecutionDays());
				}
				else
				{
					this.textBoxTrialInfo.Text = string.Format("剩余试用次数为 {0} 次", Product.GetTrialExecutionCounter());
				}
				this.textBoxTrialInfo.Visible = true;
			}
			else
			{
				this.comboBoxType.Text = text;
			}
			this.buttonShowUpdate.Text = "显示更新";
			this.buttonShowUpdate.Image = Resources.ArrowDown16;
			this.textBoxUpdateInfo.Text = this.GetUpdateInfo();
			base.CenterToParent();
			this.HideUpdateInfo();
		}

		private void SetCkeckStartVisible(bool beVisible)
		{
			this.cbStartSame.Visible = beVisible;
			this.cbStartJust.Visible = beVisible;
		}

		private void HideUpdateInfo()
		{
			base.Height = this.FormSize.Height - this.textBoxUpdateInfo.Size.Height;
			this.buttonShowUpdate.Text = "显示更新";
			this.buttonShowUpdate.Image = Resources.ArrowDown16;
			this.textBoxUpdateInfo.Visible = false;
		}

		private void buttonShowUpdate_Click(object sender, EventArgs e)
		{
			if (this.textBoxUpdateInfo.Visible)
			{
				this.HideUpdateInfo();
				return;
			}
			base.Height = this.FormSize.Height;
			this.buttonShowUpdate.Text = "隐藏更新";
			this.buttonShowUpdate.Image = Resources.ArrowDown16;
			this.textBoxUpdateInfo.Visible = true;
		}

		private void linkLabelYArchitechMainPage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			this.linkLabelYArchitechMainPage.LinkVisited = true;
			Process.Start(Product.GetHomePageURL());
		}

		private void linkLabelYArchitechForum_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			this.linkLabelYArchitechForum.LinkVisited = true;
			Process.Start(Product.GetForumURL());
		}

		private void linkLabelEmail_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			this.linkLabelEmail.LinkVisited = true;
			Process.Start(Product.GetEmailURL());
		}

		private void buttonClose_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private string GetUpdateInfo()
		{
			StreamReader streamReader = new StreamReader(Path.Combine(Product.DataLocation, "Update.txt"), Encoding.Default);
			string result = streamReader.ReadToEnd();
			streamReader.Close();
			return result;
		}

		private void AboutBox_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (!new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator))
			{
				YJKMessageBox.Show("操作失败,请用管理员权限运行！");
				return;
			}
			bool @checked = this.cbStartJust.Checked;
			bool checked2 = this.cbStartSame.Checked;
			Product.SaveJustStarting(@checked);
			Product.SaveStartingAtTheSameTime(checked2);
			if (!checked2)
			{
				Thread thread = new Thread(new ThreadStart(this.DeleteSpecHYAddInFiles));
				if (thread.ThreadState != System.Threading.ThreadState.Running)
				{
					thread.Start();
				}
			}
		}

		private void DeleteSpecHYAddInFiles()
		{
			if (Product.IsArchitecture() || Product.IsRME())
			{
				List<string> list = Product.GetSourceAddInFile(true).ToList<string>();
				if (list != null)
				{
					List<string> platformAllAddInFolders = Product.GetPlatformAllAddInFolders();
					if (platformAllAddInFolders != null && platformAllAddInFolders.Count > 0)
					{
						foreach (string path in platformAllAddInFolders)
						{
							Product.GetOrDelAddinFile(path, list, true);
						}
					}
				}
			}
		}

		private void DeleteNotHYAddinFiles()
		{
			List<string> list = null;
			if (Product.IsArchitecture())
			{
				list = Product.GetHYRMESourceAddInFile().ToList<string>();
			}
			else if (Product.IsRME())
			{
				list = Product.GetYJKArchSourceAddInFile().ToList<string>();
			}
			if (list != null)
			{
				List<string> platformAllAddInFolders = Product.GetPlatformAllAddInFolders();
				if (platformAllAddInFolders != null && platformAllAddInFolders.Count > 0)
				{
					foreach (string path in platformAllAddInFolders)
					{
						Product.GetOrDelAddinFile(path, list, true);
					}
				}
			}
			Product.CopySpecAddInFile();
			if (this.cbStartJust.Checked)
			{
				List<string> list2 = Product.GetSourceAddInFile(true).ToList<string>();
				if (list2 != null)
				{
					List<string> platformAllAddInFolders2 = Product.GetPlatformAllAddInFolders();
					if (platformAllAddInFolders2 != null && platformAllAddInFolders2.Count > 0)
					{
						foreach (string path2 in platformAllAddInFolders2)
						{
							Product.GetOrDelAddinFile(path2, list2, false);
						}
					}
					Product.DelAddinFile(Product.GetUserAddInFolder());
				}
			}
		}

		private Size textboxUpdateInfoSize = new Size(0, 0);

		private Size FormSize = new Size(0, 0);
	}
}
